﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GeoTrans;
using GpSpotMe.Ign.TileModel;


namespace WinTest.GpSpotMe.Ign.TileModel
{
    public class IgnMap
    {
        Point utmCoord;
        Bitmap bmp;
        ITileSource tileSource = new DirectTileManager();

        public IgnMap(int i, int j, Bitmap bmp)
        {
            this.utmCoord = new Point(i, j);
            this.bmp = bmp ;
        }

        public IgnMap(Point utmCoord, Bitmap bmp)
        {
            this.utmCoord = utmCoord;
            this.bmp = bmp;
        }

        /// <summary>
        /// Crea un mapBox del numero de tiles
        /// Centrado en el punto que se pasa.
        /// </summary>
        public IgnMap(Point utmCoord, int size, int nt)
        {
            Point centralPoint = Converter.UtmToTile(utmCoord);
            Point refPoint = new Point();

            //Calculo del punto de referencia
            if (nt % 2 == 1) //impar
            {
                int d = nt / 2;
                refPoint.X = centralPoint.X - d;
                refPoint.Y = centralPoint.Y + d;
            }
            else //Pares
            {
                Point pxPoint = Converter.UtmToPx(utmCoord);
                Point pxRefPoint = Converter.TileToPx(centralPoint);
                int dx = pxPoint.X - pxRefPoint.X;
                int dy = pxRefPoint.Y - pxPoint.Y;
                //2 tiles
                if (dx < 128 && dy < 128)
                {
                    refPoint.X = centralPoint.X - 1;
                    refPoint.Y = centralPoint.Y + 1;
                }
                else if (dx >= 128 && dy < 128)
                {
                    refPoint.X = centralPoint.X;
                    refPoint.Y = centralPoint.Y + 1;
                }
                else if (dx >= 128 && dy >= 128)
                {
                    refPoint.X = centralPoint.X;
                    refPoint.Y = centralPoint.Y;
                }
                else
                {
                    refPoint.X = centralPoint.X - 1;
                    refPoint.Y = centralPoint.Y;
                }
                int d = (nt - 2) / 2;
                if (d > 0)
                {
                    refPoint.X = refPoint.X - d;
                    refPoint.Y = refPoint.Y + d;
                }
            }
            Bitmap mapaGrande = new Bitmap(nt * 256, nt * 256);
            using (Graphics mapGrph = Graphics.FromImage(mapaGrande))
            {
                mapGrph.FillRectangle(new SolidBrush(Color.Coral), 0, 0, mapaGrande.Height, mapaGrande.Width);
                for (int di = 0; di < nt; di++)
                {
                    for (int dj = 0; dj < nt; dj++)
                    {
                        Tile tile = tileSource.GetTile(refPoint.X + di, refPoint.Y - dj);
                        mapGrph.DrawImage(tile.Bmp, di * 256, dj * 256);
                    }
                }
                Point pxPoint = Converter.UtmToPx(utmCoord);
                Point pxRefPoint = Converter.TileToPx(refPoint);
                int dX = pxPoint.X - pxRefPoint.X;
                int dy = pxRefPoint.Y - pxPoint.Y;
                Pen pen = new Pen(Color.Blue, 2);
                mapGrph.DrawEllipse(pen, dX - 4, dy - 4, 8, 8);
                pen.Width = 1;
                mapGrph.DrawEllipse(pen, dX - 8, dy - 8, 16, 16);
            }
            this.utmCoord = Converter.PxToUtm(Converter.TileToPx(refPoint));
            this.bmp = mapaGrande;
        }


        public int UtmCoordX
        {
            get { return this.utmCoord.X; }
        }
        
        public int UtmCoordY
        {
            get { return this.utmCoord.Y; }
        }
        
        public Bitmap Bmp
        {
            get { return this.bmp; }
        }
    }
}
